package com.yix.admin.modules.system.controller;

import com.yix.admin.modules.system.entity.SysDict;
import com.yix.admin.modules.system.entity.SysDictDetail;
import com.yix.admin.modules.system.entity.dto.SysDictDetailDto;
import com.yix.admin.modules.system.entity.query.SysDictDetailQuery;
import com.yix.admin.modules.system.service.SysDictDetailService;
import com.yix.common.exception.BadRequestException;
import com.yix.logging.annotation.LogAop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/2/5 14:21
 */
@RestController
@Api(tags = "系统：字典详情管理")
@RequestMapping("/api/dictDetail")
public class SysDictDetailController {

    private static final String ENTITY_NAME = "dictDetail";

    @Autowired
    private SysDictDetailService sysDictDetailService;

    @ApiOperation("查询字典详情")
    @GetMapping
    public ResponseEntity<Object> query(SysDictDetailQuery criteria,
                                        @PageableDefault(sort = {"dictSort"}, direction = Sort.Direction.ASC) Pageable pageable){
        return new ResponseEntity<>(sysDictDetailService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @ApiOperation("查询多个字典详情")
    @GetMapping(value = "/map")
    public ResponseEntity<Object> getDictDetailMaps(@RequestParam String dictName){
        String[] names = dictName.split("[,，]");
        Map<String, List<SysDictDetailDto>> dictMap = new HashMap<>(16);
        for (String name : names) {
            dictMap.put(name, sysDictDetailService.getDictByName(name));
        }
        return new ResponseEntity<>(dictMap, HttpStatus.OK);
    }

    @LogAop("新增字典详情")
    @ApiOperation("新增字典详情")
    @PostMapping
    @PreAuthorize("@el.check('dict:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SysDictDetail resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        sysDictDetailService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @LogAop("修改字典详情")
    @ApiOperation("修改字典详情")
    @PutMapping
    @PreAuthorize("@el.check('dict:edit')")
    public ResponseEntity<Object> update(@Validated(SysDictDetail.Update.class) @RequestBody SysDictDetail resources){
        sysDictDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @LogAop("删除字典详情")
    @ApiOperation("删除字典详情")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("@el.check('dict:del')")
    public ResponseEntity<Object> delete(@PathVariable Long id){
        sysDictDetailService.delete(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
